package zozopo.operations.business.ad;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.comm.CNSpell;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.business.AdTB;
import zozopo.operations.business.StoreTB;
import zozopo.operations.comm.DictionaryOS;
import zozopo.operations.comm.OperatingService;

public class GoodsOS extends OperatingService{
	private static final Log logger = LogFactory.getLog(GoodsOS.class);
	public GoodsOS(Connection conn){
		super(conn);
	}
	
	public GoodsOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insertGoods(GoodsTB goods){
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		//insertMap.put(GoodsTB.ID_GOODS, goods.getImage_goods());
		insertMap.put(GoodsTB.USERMAIL_GOODS, goods.getUseremail_goods());
		insertMap.put(GoodsTB.URL_GOODS, goods.getUrl_goods());
		insertMap.put(GoodsTB.STORE_GOODS, goods.getStore_goods());
		//insertMap.put(GoodsTB.IMAGE_GOODS, goods.getImage_goods());
		insertMap.put(GoodsTB.HTMLCODE_GOODS, goods.getHtmlcode_goods());
		//insertMap.put(GoodsTB.CONTENT_GOODS, goods.getContent_goods());
		insertMap.put(GoodsTB.NAME_GOODS, goods.getName_goods());
		//insertMap.put(GoodsTB.KIND_GOODS, goods.getKind_goods());
		insertMap.put(GoodsTB.LABLE_GOODS, goods.getLable_goods());
		//insertMap.put(GoodsTB.FROM_GOODS, goods.getFrom_goods());
		//insertMap.put(GoodsTB.DATE_GOODS, goods.getDate_goods());
		//insertMap.put(GoodsTB.BRAND_GOODS, goods.getBrand_goods());
		insertMap.put(GoodsTB.CNBRAND_GOODS, goods.getCnbrand_goods());
		insertMap.put(GoodsTB.ENBRAND_GOODS, goods.getEnbrand_goods());
		insertMap.put(GoodsTB.TYPE_GOODS, goods.getType_goods());
		//insertMap.put(GoodsTB.VIEWS_GOODS, goods.getViews_goods());

		return sqlMachine.insertRow(GoodsTB.TABLE_NAME, insertMap);
	
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean deleteGoodsById(int id){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return sqlMachine.deleteRow(GoodsTB.TABLE_NAME, GoodsTB.ID_GOODS, id);
	}
	
	/**
	 * @version 1.0
	 * @param ReleaseTB release
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see insertRelease(ImageTB image)
	 */
	public boolean upadteGoods(GoodsTB goods){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(GoodsTB.ID_GOODS, goods.getId_goods());
		
		//insertMap.put(GoodsTB.ID_GOODS, goods.getImage_goods());
		updateMap.put(GoodsTB.USERMAIL_GOODS, goods.getUseremail_goods());
		updateMap.put(GoodsTB.URL_GOODS, goods.getUrl_goods());
		updateMap.put(GoodsTB.STORE_GOODS, goods.getStore_goods());
		//insertMap.put(GoodsTB.IMAGE_GOODS, goods.getImage_goods());
		updateMap.put(GoodsTB.HTMLCODE_GOODS, goods.getHtmlcode_goods());
		//insertMap.put(GoodsTB.CONTENT_GOODS, goods.getContent_goods());
		updateMap.put(GoodsTB.NAME_GOODS, goods.getName_goods());
		//insertMap.put(GoodsTB.KIND_GOODS, goods.getKind_goods());
		updateMap.put(GoodsTB.LABLE_GOODS, goods.getLable_goods());
		//insertMap.put(GoodsTB.FROM_GOODS, goods.getFrom_goods());
		//insertMap.put(GoodsTB.DATE_GOODS, goods.getDate_goods());
		//insertMap.put(GoodsTB.BRAND_GOODS, goods.getBrand_goods());
		updateMap.put(GoodsTB.CNBRAND_GOODS, goods.getCnbrand_goods());
		updateMap.put(GoodsTB.ENBRAND_GOODS, goods.getEnbrand_goods());
		updateMap.put(GoodsTB.TYPE_GOODS, goods.getType_goods());
		//insertMap.put(GoodsTB.VIEWS_GOODS, goods.getViews_goods());
		
		return sqlMachine.updateRow(GoodsTB.TABLE_NAME, updateMap, GoodsTB.ID_GOODS, goods.getId_goods());
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsTB> selectAllGoods(){
		String sql = "SELECT * FROM " + GoodsTB.TABLE_NAME;
		return selectGoodsBySql(sql);
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public GoodsTB getGoodsById(int id){
		String sql = "SELECT * FROM " + GoodsTB.TABLE_NAME + " where " + GoodsTB.ID_GOODS + "=" + id;
		GoodsTB goods = new GoodsTB();
		List<GoodsTB> goodsList = selectGoodsBySql(sql);
		goods = goodsList.get(0);
		
		return goods;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsTB> selectGoodsByCnbrandAndEnbrandAndType(String cnbrand,String endbrand,String type,int beginPageNum,int pageSize){
		String sql = "SELECT * FROM " + GoodsTB.TABLE_NAME + " where " + GoodsTB.CNBRAND_GOODS + "='" + cnbrand + "' "
		+ " OR " + GoodsTB.ENBRAND_GOODS +"='"+endbrand+"'" + " OR " + GoodsTB.TYPE_GOODS +"='"+type+"'";
		//GoodsTB goods = new GoodsTB();
		return selectGoodsBySql(sql);
		
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsTB> selectGoodsByCnbrandAndEnbrandAndTypeAndLable(String cnbrand,String endbrand,String type,String lable,int beginPageNum,int pageSize){
		String sql = "SELECT * FROM " + GoodsTB.TABLE_NAME + " where " + GoodsTB.CNBRAND_GOODS + "='" + cnbrand + "' "
		+ " OR " + GoodsTB.ENBRAND_GOODS +"='"+endbrand+"'" + " OR " + GoodsTB.TYPE_GOODS +"='"+type+"'" 
		+ " AND " + GoodsTB.LABLE_GOODS + "='"+lable+"'";
		//GoodsTB goods = new GoodsTB();
		return selectGoodsBySql(sql);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsTB> selectGoodsByCnbrandAndEnbrand(String cnbrand,String enbrand,int beginPageNum,int pageSize){
		//修正品牌为空时找出非正确结果
		if(cnbrand == null || cnbrand.trim().equals("")){
			cnbrand = enbrand;
		}
		if(enbrand == null || enbrand.trim().equals("")){
			enbrand = cnbrand;
		}
		String sql = "SELECT * FROM " + GoodsTB.TABLE_NAME + " where " + GoodsTB.CNBRAND_GOODS + "='" + cnbrand + "' "
		+ " OR " + GoodsTB.ENBRAND_GOODS +"='"+enbrand+"'";
		//GoodsTB goods = new GoodsTB();
		return selectGoodsBySql(sql);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsTB> selectGoodsByCnbrandAndEnbrandAndLable(String cnbrand,String endbrand,String lable,int beginPageNum,int pageSize){
		String sql = "SELECT * FROM " + GoodsTB.TABLE_NAME + " where " + GoodsTB.CNBRAND_GOODS + "='" + cnbrand + "' "
		+ " OR " + GoodsTB.ENBRAND_GOODS +"='"+endbrand+"'" 
		+ " AND " + GoodsTB.LABLE_GOODS + "='"+lable+"'";
		//GoodsTB goods = new GoodsTB();
		return selectGoodsBySql(sql);
	}
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + GoodsTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	public List<GoodsTB> selectGoodsTB(int beginPageNum,int pageSize){
		
		List<GoodsTB> goodsList = null;
		String sql = "SELECT * FROM " + GoodsTB.TABLE_NAME  +" limit "+beginPageNum+","+pageSize; 
		goodsList = selectGoodsBySql(sql);
		
		return goodsList;
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsTB> selectGoodsBySql(String sql){
		List<GoodsTB> goodsList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		DictionaryOS dictionaryOS= new DictionaryOS(conn);
		if(resultSet != null){
				try {
					goodsList = new ArrayList<GoodsTB>();
					while(resultSet.next()){
						GoodsTB goods = new GoodsTB();
						
						
						goods.setId_goods(resultSet.getInt(GoodsTB.ID_GOODS));
					//	goods.setBrand_goods(resultSet.getString(GoodsTB.BRAND_GOODS));
						goods.setCnbrand_goods(resultSet.getString(GoodsTB.CNBRAND_GOODS));
						goods.setDate_goods(resultSet.getString(GoodsTB.DATE_GOODS));
						goods.setEnbrand_goods(resultSet.getString(GoodsTB.ENBRAND_GOODS));
						goods.setHtmlcode_goods(resultSet.getString(GoodsTB.HTMLCODE_GOODS));
						goods.setLable_goods(resultSet.getString(GoodsTB.LABLE_GOODS));
						goods.setName_goods(resultSet.getString(GoodsTB.NAME_GOODS));
						goods.setStore_goods(resultSet.getString(GoodsTB.STORE_GOODS));
						goods.setUrl_goods(resultSet.getString(GoodsTB.URL_GOODS));
						goods.setType_goods(resultSet.getString(GoodsTB.TYPE_GOODS));
						goods.setViews_goods(resultSet.getInt(GoodsTB.VIEWS_GOODS));
						
							
						goods.setLablevalue(dictionaryOS.getDictValue(goods.getLable_goods()));//为了显示
						//store.setKindvalue(dictionaryOS.getLableValue(store.getKind_store()));//为了显示
						
						goodsList.add(goods);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return goodsList;
	
	}
}
